package com.example.prison.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.SpecialInfo;
import com.example.prison.service.SpecialInfoService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "员工特殊职位管理")
@RestController
@RequestMapping("/api")
public class SpecialInfoController {

    @Autowired
    private SpecialInfoService specialInfoService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取特殊职位列表")
    @GetMapping("/getSpecialPost")
    public Response<PageInfo<SpecialInfo>> getSpecialPost(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                      @RequestParam(required = false)@Parameter(description = "职位") String post) {
        PageInfo<SpecialInfo> pageInfo = specialInfoService.get(pageNumber, pageSize, post);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增特殊职位")
    @PostMapping("/addSpecialPost")
    public Response<?> addSpecialPost(@RequestBody SpecialInfo data) {
        int result = specialInfoService.add(data);
        if (result == -1) {
            return Response.error("该特殊职位已存在！");
        }
        log.info("新增特殊职位"+", 数据："+ JSON.toJSONString(data));
        systemLogService.add("新增特殊职位");
        return Response.success("新增特殊职位成功！");
    }

    @ApiOperation("依据id删除特殊职位数据")
    @PostMapping("/deleteSpecialPost")
    public Response<?> deleteSpecialPost(@Parameter(description = "ID") @RequestBody Integer[] ids) {
        specialInfoService.deleting(ids);
        log.info("删除特殊职位数据");
        systemLogService.add("删除特殊职位数据");
        return Response.success("删除成功！");

    }

    @ApiOperation("编辑特殊职位")
    @PostMapping("/updateSpecialPost")
    public Response<?> updateSpecialPost(@RequestBody SpecialInfo data) {
        Integer id = data.getId();
        int result = specialInfoService.updating(data, id);
        if (result == 0) {
            return Response.error("该特殊职位已存在！");
        }
        log.info("编辑特殊职位"+", 数据："+JSON.toJSONString(data));
        systemLogService.add("编辑特殊职位");
        return Response.success("更新成功！");
    }

    @ApiOperation("依据id获取特殊职位信息")
    @GetMapping("/getSpecialPostById")
    public Response<SpecialInfo> getSpecialPostById(@Parameter(description = "ID")@RequestParam Integer id) {
        SpecialInfo data = specialInfoService.getById(id);
        if(ObjectUtils.isEmpty(data)) {
            return Response.error("此特殊职位id不存在！");
        }
        return Response.success("获取成功！", data);
    }

    @ApiOperation("检查部门特定职位")
    @GetMapping("/checkSpecialPost")
    public Response<?> checkSpecialPost() {
        String result = specialInfoService.checkSpecialPost();
        return Response.success("获取成功！", result);
    }

}
